package com.pets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pets.pojo.Users;
import com.pets.mapper.UsersMapper;
import com.pets.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pets.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Gikx
 * @since 2024-04-04
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public Result login(String username, String password, HttpSession session) {
        LambdaQueryWrapper<Users> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Users::getUsername, username)
                .eq(Users::getPassword, password);
        final Users users = usersMapper.selectOne(lqw);
        if (users != null) {
            session.setAttribute("user",users);
            return Result.success("登陆成功", users);
        } else {
            return Result.fail("登陆失败");
        }
    }

    @Override
    public Result register(Users users) {
        if (users.getUsername().isEmpty() || users.getUsername() == null ||
                users.getPassword().isEmpty() || users.getPassword() == null) {
            return Result.fail("账号密码不能为空和null");
        }
        //查询判断当前用户是否已存在
        final LambdaQueryWrapper<Users> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Users::getUsername, users.getUsername());
        Users user1 = usersMapper.selectOne(lqw);
        if (user1 != null)
            return Result.fail("当前用户已存在，请重试");
        if (users.getAvatar() == null || users.getAvatar().isEmpty())
            users.setAvatar("http://localhost:1305/staticfiles/tmp/Avatar.jpeg");
        if (users.getGender() == null || users.getGender().isEmpty())
            users.setGender("1");
        if(users.getUserType()==null){
            users.setUserType(2);//2为普通用户
        }
        users.setVipNumber(0);//默认会员卡数量为0
        return usersMapper.insert(users) > 0 ? Result.success("注册成功") : Result.fail("注册失败");
    }

    @Override
    public Result deleteById(Integer id) {
        Users usersInfo = usersMapper.selectById(id);
        if(usersInfo.getUserId()==1)
            return Result.error("不能删除管理员！");
        int rows = usersMapper.deleteById(id);
        return rows > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Override
    public Result update(Users users) {
        if(users.getVipNumber()!=null&&users.getVipNumber()>0&&users.getUserType()== 2){
            users.setUserType(3);
        }
        int rows = usersMapper.updateById(users);
        return rows > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @Override
    public Result selectById(Integer id) {
        final Users users = usersMapper.selectById(id);
        return users != null ? Result.success("查询成功", users) : Result.error("查询失败");
    }

    @Override
    public Result selectPage(Integer page, Integer pageSize, String username, String nickname, Integer userType, String gender) {
        final LambdaQueryWrapper<Users> lqw = new LambdaQueryWrapper<>();
        lqw.like(nickname != null && !nickname.isEmpty(), Users::getNickname, nickname)
                .eq(username != null && !username.isEmpty(), Users::getUsername, username)
                .eq(userType != null , Users::getUserType, userType)
                .eq(gender != null && !gender.isEmpty(), Users::getGender, gender);
        IPage<Users> Page = new Page<>(page, pageSize);
        IPage<Users> IPage = usersMapper.selectPage(Page, lqw);
        return Result.success("查询成功", IPage);
    }
}
